package com.nervenets.shopping.controller.common;

import com.nervenets.general.entity.ResponseResult;
import com.nervenets.general.jwt.aspect.JwtRole;
import com.nervenets.general.jwt.aspect.JwtSecurity;
import com.nervenets.general.jwt.util.JwtUtils;
import com.nervenets.general.utils.JodaUtils;
import com.nervenets.general.web.BaseEntityController;
import com.nervenets.general.web.params.IdsParams;
import com.nervenets.shopping.controller.common.params.FeedbackFormParams;
import com.nervenets.shopping.controller.common.params.FeedbackHandleParams;
import com.nervenets.shopping.controller.common.params.FeedbackPagingParams;
import com.nervenets.shopping.hibernate.domain.common.Feedback;
import com.nervenets.shopping.hibernate.domain.user.Manager;
import com.nervenets.shopping.hibernate.domain.user.User;
import com.nervenets.shopping.service.FeedbackService;
import com.nervenets.shopping.service.ManagerService;
import com.nervenets.shopping.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Api(value = "投诉建议接口v1", tags = "投诉建议相关接口")
@RestController
@RequestMapping("/api/feedback")
//@JwtRole(group = "system", groupName = "系统管理", function = "feedback", functionName = "投诉建议")
public class FeedbackController extends BaseEntityController<Feedback, FeedbackService, FeedbackFormParams, FeedbackPagingParams, Feedback, Feedback> {
    @Resource
    private UserService userService;
    @Resource
    private ManagerService managerService;

    @Override
    protected Feedback generateListResult(Feedback feedback, FeedbackPagingParams params, HttpServletRequest request, HttpServletResponse response) {
        switch (feedback.getUnionType()) {
            case normal:
                break;
            case manager:
                break;
            case user:
                break;
            case flows:
                break;
            case order:
                break;
            case product:
                break;
            case coupon:
                break;
        }
        if (feedback.getCreateBy() > 0) {
            User user = userService.fastFind(feedback.getCreateBy());
            if (null != user) {
                feedback.setCreateUserName(user.getName());
                feedback.setCreateUserAvatar(user.getAvatar());
            }
        }
        if (feedback.getHandleUserId() > 0) {
            Manager manager = managerService.fastFind(feedback.getHandleUserId());
            if (null != manager) {
                feedback.setHandleUserName(manager.getName());
            }
        }
        return feedback;
    }

    @Override
    public ResponseEntity<ResponseResult<Feedback>> edit(@Valid @RequestBody FeedbackFormParams params, HttpServletRequest request, HttpServletResponse response) {
        return super.edit(params, request, response);
    }

    @Override
    public ResponseEntity<ResponseResult<Void>> del(@Valid @RequestBody IdsParams params, HttpServletRequest request, HttpServletResponse response) {
        return super.del(params, request, response);
    }

    @ApiOperation(value = "处理反馈")
    @PostMapping("/handle")
    @JwtSecurity(permission = "handle", permissionName = "处理")
    public ResponseEntity<ResponseResult<Feedback>> handle(@Valid @RequestBody FeedbackHandleParams params, HttpServletRequest request) {
        Feedback feedback = service.findOne(params.getTargetId());
        if (null != feedback) {
            feedback.setHandleRemark(params.getHandleRemark());
            feedback.setHandleUserId(JwtUtils.getUser().getId());
            feedback.setHandleTime(JodaUtils.getTimestamp());
            service.save(feedback);
        }
        return successMessage(feedback);
    }
}
